package org.clockwise.driver;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.clockwise.util.MyFileReader;

public class DataReader {
	
	public static int readData(List<List<List<double[]>>> data, List<List<List<Long>>> dict) {
		int all = 0;
		String source = LinkerDriver.DATA_DIR + "pureData.txt";
		int checkDataResult = DataReader.checkData(source);
		if (checkDataResult > 0) {
			System.out.println("Line " + checkDataResult + " is illegal!");
			return all;
		}
		
		MyFileReader mfr = new MyFileReader(source);
		
		String temp = null;
		int prevContextId = Integer.MAX_VALUE, prevQueryId = Integer.MAX_VALUE;
		List<List<double[]>> context = null;
		List<List<Long>> cUser = null;
		List<double[]> query = null;
		List<Long> qUser = null;
		while ((temp = mfr.getNextLine()) != null) {
			String[] seg = temp.split("\t");
			if (seg.length < 4) {
				continue;
			}
			try {
				
				int contextId = Integer.parseInt(seg[0]);
				int queryId = Integer.parseInt(seg[1]);
				Long id = Long.parseLong(seg[2]);
				double[] feature = new double[seg.length - 3];
				for (int i = 0; i < feature.length; i++) {
					feature[i] = Double.parseDouble(seg[i + 3]);
				}
				
				if (prevContextId != contextId || prevContextId == Integer.MAX_VALUE) {
					prevContextId = contextId;
					prevQueryId = queryId;
					if (query != null && query.size() > 0) {
						context.add(query);
						cUser.add(qUser);
					}
					if (context != null && context.size() > 0) {
						data.add(context);
						dict.add(cUser);
					}
					context = new ArrayList<List<double[]>>();
					query = new ArrayList<double[]>();
					cUser = new ArrayList<List<Long>>();
					qUser = new ArrayList<Long>();
				}
				else if (prevQueryId != queryId || prevQueryId == Integer.MAX_VALUE) {
					prevQueryId = queryId;
					if (query != null && query.size() > 0) {
						context.add(query);
						cUser.add(qUser);
					}
					query = new ArrayList<double[]>();
					qUser = new ArrayList<Long>();
				}
				query.add(feature);
				qUser.add(id);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		mfr.close();
		
		Iterator<List<List<double[]>>> iter = data.iterator();
		while (iter.hasNext()) {
			all += iter.next().size();

		}
		
		return all;
	}
	
	public static int checkData(String file) {
		MyFileReader mfr = new MyFileReader(file);
		int dim = Integer.MAX_VALUE;
		String temp = null;
		int line = 0;
		while ((temp = mfr.getNextLine()) != null) {
			line++;
			String[] seg = temp.split("\t");
			if (seg.length < 3)
				return line;
			if (dim == Integer.MAX_VALUE) {
				dim = seg.length;
			}
			else if (dim != seg.length) {
				return line;
			}
			try {
				Integer.parseInt(seg[0]);
				Integer.parseInt(seg[1]);
				for (int i = 2; i < seg.length; i++) {
					Double.parseDouble(seg[i]);
				}
			} catch (NumberFormatException e) {
				return line;
			}
		}
		mfr.close();
		return 0;
	}
}
